package wcg;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.Point;

import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.awt.GridLayout;

public class CsvOptionDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton jButtonOK = null;
	private JButton jButtonCancel = null;
	public boolean CsvHeader = true;
	public boolean CsvTime = true;
	public boolean MillisecondSepComma = false;
	public boolean LittleEndian = true;
	public boolean Physical = false;
	public boolean OK=false;
	public int Decimation = 1;
	public int DecimationMode = 0;
	private int Mode=0; // 0=CSV , 1=Binary
	private JCheckBox jCheckBoxHeader = null;
	private JCheckBox jCheckBoxTime = null;
	private JCheckBox jCheckBoxMillisecondComma = null;
	private JTextField jTextFieldTimeOffset = null;
	private JLabel jLabel = null;
	public  double TimeShift = 0.0;
	private JTextField textFieldDecimation;
	private JComboBox comboBoxDecimationMode;
	private JLabel jLabelDisplayGroup = null;
	private JLabel jLabelDisplayGroup1 = null;
	private JCheckBox jCheckBoxLittleEndian = null;
	private JPanel panel_1;
	private JPanel panel_2;
	private JPanel panel_3;
	public JCheckBox cbPhysicalQuantity ;
	
	public void setMode(int mode){
		Mode=mode;
		if(mode==0){
			jCheckBoxHeader.setEnabled(true);
			jCheckBoxTime.setEnabled(true);
			jCheckBoxMillisecondComma.setEnabled(true);
			jCheckBoxLittleEndian.setEnabled(false);
		}
		else{
			jCheckBoxHeader.setEnabled(false);
			jCheckBoxTime.setEnabled(false);
			jCheckBoxMillisecondComma.setEnabled(false);
			jCheckBoxLittleEndian.setEnabled(true);
		}
	}
	/**
	 * @param owner
	 */
	public CsvOptionDialog(Frame owner) {
		super(owner,true);
		initialize();
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE );
		if(owner!=null){
			Point p = owner.getLocationOnScreen();
			Dimension d = owner.getSize();
			Dimension dm = this.getSize();
			p.x += (d.width-dm.width)/2;
			p.y += (d.height-dm.height)/2;
			setLocation(p);
		}
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(385, 375);
		this.setTitle("Data output CSV/Binary Option");
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			
			JPanel panel = new JPanel();
			panel.setBorder(new TitledBorder(null, "CSV", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panel.setBounds(12, 9, 181, 164);
			jContentPane.add(panel);
			panel.setLayout(null);
			panel.add(getJCheckBoxHeader());
			panel.add(getJCheckBoxTime());
			panel.add(getJCheckBoxMillisecondComma());
			
			cbPhysicalQuantity = new JCheckBox("Physical Quantity");
			cbPhysicalQuantity.setBounds(8, 124, 150, 21);
			panel.add(cbPhysicalQuantity);
			jContentPane.add(getPanel_1());
			jContentPane.add(getPanel_2());
			jContentPane.add(getPanel_3());
		}
		return jContentPane;
	}

	public void updateData(boolean set){
		if(set){
			getJCheckBoxHeader().setSelected(CsvHeader);
			getJCheckBoxTime().setSelected(CsvTime);
			getJCheckBoxMillisecondComma().setSelected(MillisecondSepComma);
			jCheckBoxMillisecondComma.setEnabled(jCheckBoxTime.isSelected()&&Mode==0);
			getJTextFieldTimeOffset().setText(Double.toString(TimeShift));
			textFieldDecimation.setText(Integer.toString(Decimation));
			comboBoxDecimationMode.setSelectedIndex(DecimationMode);
			getJCheckBoxLittleEndian().setSelected(LittleEndian);
			cbPhysicalQuantity.setSelected(Physical);
			
		}
		else{
			CsvHeader = getJCheckBoxHeader().isSelected();
			CsvTime   = getJCheckBoxTime().isSelected();
			MillisecondSepComma = getJCheckBoxMillisecondComma().isSelected();
			TimeShift = Double.valueOf(getJTextFieldTimeOffset().getText());
			Decimation = Integer.valueOf(textFieldDecimation.getText());
			DecimationMode = comboBoxDecimationMode.getSelectedIndex();
			LittleEndian = getJCheckBoxLittleEndian().isSelected();
			Physical = cbPhysicalQuantity.isSelected();
		}
	}
	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setPreferredSize(new Dimension(80,30));
			jButtonOK.setText("OK");
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					OK=true;
					setVisible(false);
				}
			});
		}
		return jButtonOK;
	}

	/**
	 * This method initializes jButtonCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setPreferredSize(new Dimension(80,30));
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					OK=false;
					setVisible(false);
				}
			});
		}
		return jButtonCancel;
	}
	/**
	 * This method initializes jCheckBoxHeader	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxHeader() {
		if (jCheckBoxHeader == null) {
			jCheckBoxHeader = new JCheckBox();
			jCheckBoxHeader.setBounds(8, 32, 97, 21);
			jCheckBoxHeader.setText("Header");
			jCheckBoxHeader.setToolTipText("Output header column");

		}
		return jCheckBoxHeader;
	}
	/**
	 * This method initializes jCheckBoxTime	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxTime() {
		if (jCheckBoxTime == null) {
			jCheckBoxTime = new JCheckBox();
			jCheckBoxTime.setBounds(8, 55, 97, 21);
			jCheckBoxTime.setText("Time");
			jCheckBoxTime.setToolTipText("Output time field");
			jCheckBoxTime.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					jCheckBoxMillisecondComma.setEnabled(jCheckBoxTime.isSelected());
				}
			});
		}
		return jCheckBoxTime;
	}

	/**
	 * This method initializes jCheckBoxMillisecondComma	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxMillisecondComma() {
		if (jCheckBoxMillisecondComma == null) {
			jCheckBoxMillisecondComma = new JCheckBox();
			jCheckBoxMillisecondComma.setBounds(18, 79, 155, 23);
			jCheckBoxMillisecondComma.setText("Separate millisecond");
			jCheckBoxMillisecondComma.setToolTipText("Time format ...second.millisecond or ...second,millisecond ");
		}
		return jCheckBoxMillisecondComma;
	}

	/**
	 * This method initializes jTextFieldTimeOffset	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldTimeOffset() {
		if (jTextFieldTimeOffset == null) {
			jTextFieldTimeOffset = new JTextField();
			jTextFieldTimeOffset.setBounds(118, 6, 71, 22);
		}
		return jTextFieldTimeOffset;
	}

	/**
	 * This method initializes jCheckBoxLittleEndian	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxLittleEndian() {
		if (jCheckBoxLittleEndian == null) {
			jCheckBoxLittleEndian = new JCheckBox();
			jCheckBoxLittleEndian.setBounds(8, 26, 113, 25);
			jCheckBoxLittleEndian.setText("LittleEndian");  // Generated
			jCheckBoxLittleEndian.setToolTipText("Byte order  ");  // Generated
		}
		return jCheckBoxLittleEndian;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setBorder(new TitledBorder(null, "Binary", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panel_1.setBounds(205, 10, 135, 63);
			panel_1.setLayout(null);
			panel_1.add(getJCheckBoxLittleEndian());
		}
		return panel_1;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setBounds(11, 183, 329, 91);
			panel_2.setLayout(null);
			jLabel = new JLabel();
			jLabel.setBounds(12, 10, 94, 18);
			panel_2.add(jLabel);
			jLabel.setText("Time Shift [s]");  // Generated
			panel_2.add(getJTextFieldTimeOffset());
			
			JLabel lblDecimate = new JLabel("Decimate ");
			lblDecimate.setBounds(16, 51, 67, 24);
			panel_2.add(lblDecimate);
			lblDecimate.setToolTipText("1 to 1000");
			
			JLabel label = new JLabel("1/");
			label.setBounds(76, 51, 30, 24);
			panel_2.add(label);
			label.setHorizontalAlignment(SwingConstants.RIGHT);
			
			textFieldDecimation = new JTextField();
			textFieldDecimation.setBounds(118, 52, 71, 24);
			panel_2.add(textFieldDecimation);
			textFieldDecimation.setColumns(10);
			
			comboBoxDecimationMode = new JComboBox();
			comboBoxDecimationMode.setBounds(207, 53, 109, 22);
			panel_2.add(comboBoxDecimationMode);
			comboBoxDecimationMode.setModel(new DefaultComboBoxModel(new String[] {"Simple","Median","Arithmetic Mean"}));
			comboBoxDecimationMode.setSelectedIndex(0);
			comboBoxDecimationMode.setToolTipText("Select decimation mode");
		}
		return panel_2;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setBounds(134, 293, 205, 30);
			panel_3.setLayout(new GridLayout(1, 0, 20, 0));
			panel_3.add(getJButtonCancel());
			panel_3.add(getJButtonOK());
		}
		return panel_3;
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
